$(document).ready(function() {

//alert('object_client_query');
	$( "#owner" ).autocomplete({
            source: function(request, response ) {
                $.ajax({
                    url: "/client/object_client",
                    dataType: "json",
                    data:{
						query: request.term
                    },
                    success: function( data ) {

						//principal_phone_type

						$("#owner_phone_type").val(1);
						$("#principal_phone_type").val(1);
						
                        response( $.map( data, function( item ) {
	                        return {
								label: item.name+"/"+item.recommended,
								value: item.name,
								client_id:item.client_id,
								name:item.name,
								mobile: item.mobile,
								home_phone: item.home_phone
	                        }
                        }));
                    }
                });
            },
			select: function(e, ui) {
			  //$("input:[name='owner_phone_type']").val(1);		
			  $("#client_id").val(ui.item.client_id);
			  $("#owner_mobile").val(ui.item.mobile);
			  $("#owner_phone").val(ui.item.home_phone);
			  $("#principal").val(ui.item.name);
			  $("#principal_mobile").val(ui.item.mobile);
			  $("#principal_phone").val(ui.item.home_phone);
			},
            minLength: 1
 	});





	$( "#name" ).autocomplete({
            source: function(request, response ) {
                $.ajax({
                    url: "/client/client_josn",
                    dataType: "json",
                    data:{
						query: request.term
                    },
                    success: function( data ) {
                        response( $.map( data, function( item ) {
	                        return {
								label: item.name+"   "+item.recommended,
								value: item.name,
								recommended: item.recommended
	                        }
                        }));
                    }
                });
            },
			select: function(e, ui) {
			  $("#recommended").val(ui.item.recommended);
			},
            minLength: 1
 	});
			
	$("input").keypress(function (e) {
		var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
		if (keyCode == 13){
			var i;
			for (i = 0; i < this.form.elements.length; i++){
				if (this == this.form.elements[i])
					break;
			}
			i = (i + 1) % this.form.elements.length;
			this.form.elements[i].focus();
			return false;
		}else{
			return true;
		}
	});

	$("select").keypress(function (e) {
		var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
		if (keyCode == 13){
			var i;
			for (i = 0; i < this.form.elements.length; i++){
				if (this == this.form.elements[i])
					break;
			}
			i = (i + 1) % this.form.elements.length;
			this.form.elements[i].focus();
			return false;
		}else{
			return true;
		}
	});

});


//step 1

function show_administrative_area(id) {

    $('#administrative_area_select').dialog({
        autoOpen: true,
        modal: true,
        width: 300,
        buttons: {
            "確定": function() {
                if ($('#administrative_area_value').val() != "") {

                    var h_a = $("#hous_ebuilding_administrative_area").html();


                    $('#' + id).val($('#administrative_area_value').val());


                    //alert($("#administrative_country :selected").val() );
                    //建物門牌	行政區 1
                    $.post("/address/administrative_area", {
                        id: $("#administrative_country :selected").val()
                    },
                    function(result) {
                        //alert("WTFFF");
                        if (result != "") {
                            $("#hous_ebuilding_street").html(result);
                            $("#house_building").val("");
                        }
                    });

                    ////////************************************************////////////////////                


                    $.post("/circle/circle_josn", {
                        city: $('#administrative_city').find("option:selected").text(),
                        area: $('#administrative_country').find("option:selected").val()
                    },
                    function(result) {
                        if (result != "") {

                            $("#circle").html(result);

                        }

                    });


                    ////////************************************************////////////////////                
                    //商圈1


                    ////////************************************************////////////////////





                }
                $(this).dialog("close");
            },
            "取消": function() {
                $(this).dialog("close");
            }
        }
    });

}

function administrative_area_choose() {
    /******/
    $('#administrative_area_value').val(
            $('#administrative_country').find("option:selected").text()
            );

}

function administrative_area_city_choose() {
    $.post("/address/county", {
        id: $("#administrative_city").val()
    },
    function(result) {
        if (result != "") {
            $("#administrative_country").html(result);
        }
    });
}


function show_hous_ebuilding(id){

    alert(id);
        /*
	$("#hous_ebuilding_administrative_area").val($("#administrative_area").val());
	$.post("/address/administrative_area",{
		id:$("#hous_ebuilding_administrative_area").val()
	},
	function(result){    
		if(result!=""){ 
			$("#hous_ebuilding_street").html(result);
		}
	}); 
        */
       
	$('#hous_ebuilding_select').dialog({
		autoOpen: true,
		modal: true,
		width: 600,
		buttons: {
			"確定": function () {
                            
				//$("#administrative_area").val($("#hous_ebuilding_administrative_area").find("option:selected").text());
				if($('#hous_ebuilding_address').val()!="")
					$('#'+id).val($('#hous_ebuilding_address').val());
				$(this).dialog("close");
			},
			"取消": function () {
				$(this).dialog("close");
			}
		}
	});

}